package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author lvFF
 * @Date 2022/04/26/上午 10:35;
 * Description
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerException(SQLIntegrityConstraintViolationException ex){
        //获取错误信息
        String message = ex.getMessage();//获取 报错的信息
        //解析出用户名字信息
        String username = message.split(" ")[2];
        return R.error(username + "已经存在，请不要重复添加");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> CustomException(CustomException cus){
       /* //获取错误信息
        String message = cus.getMessage();//获取 报错的信息
        //解析出用户名字信息
        String username = message.split(" ")[2];*/
        return R.error(cus.getMessage());
    }
}
